import * as enumApi from "@/api/modulManage/enum";
import * as unitUpi from "@/api/modulManage/unit"
import * as attributeApi from "@/api/modulManage/attribute"
import * as attributeClassifyApi from "@/api/modulManage/attributeClassify"
import * as lifeCycleApi from "@/api/settingManage/lifeCycle";

let apiObj = {
  "EnumManage": enumApi,
  "UnitsManage": unitUpi,
  "PropertiesClassify": attributeClassifyApi,
  "PropertiesDefined": attributeApi,
  "LifeCycle": lifeCycleApi,
  "VersionRules": lifeCycleApi,
  "CodeRules": lifeCycleApi,
}
export default {
  data() {
    return {
      api: "",
      queryForm: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 1
      },
      data: [],
      loading: false,
      selection: [],

      visible: false,
      tabsList: [],

      ifIdKey: ["LifeCycle"]
    }
  },
  computed: {
    idKey() {
      return this.ifIdKey.includes(this.$options.name) ? "id" : "oid";
    }
  },
  methods: {
    // 向抽屉中添加tab页 根据 oid 去重
    addToTabsList(title, row, component, type) {
      let ids = this.tabsList.map(item => item.row[this.idKey]);
      if (!ids.includes(row[this.idKey])) {
        this.tabsList.push({title, row, component, type})
      }
      this.visible = true;
    },
    // 删除
    remove(data) {
      this.$confirm("是否删除？").then(() => {
        let ids = data.map(item => item[this.idKey]);
        this.api.del({ids: ids.join(",")}).then(() => {
          this.$message.success("删除成功");
          this.getList();
          this.selection = [];
        })
      })
    },
    // 启用/停用
    start_stop(data, type) {
      let msg = type === "start" ? "启用成功" : "停用成功";
      let ids = data.map(item => item[this.idKey]);
      this.api[type]({ids: ids.join(',')}).then(() => {
        this.$message.success(msg);
        this.getList();
      })
    },
    getList() {
      this.loading = true;
      this.api.getList({page: this.page.currentPage, pageSize: this.page.pageSize, ...this.queryForm}).then(res => {
        const {list, totalRows} = res.data.data;
        this.page.total = totalRows;
        this.data = list;
        this.loading = false;
      })
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList();

    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList();
    },
  },
  mounted() {
    this.api = apiObj[this.$options.name];
    this.getList();
  }
}
